package yehang.shared.entity;

import org.slf4j.MDC;
import yehang.shared.exception.BizCodeEnume;

/**
 * @author: yehang
 * 2021/7/26 10:59
 */
public class CommonResult<T> {

    private Integer code = 200;
    private boolean success = true;
    private String message;
    private String traceId;
    /**
     * 错误的根本原因
     */
    private String cause;
    private T data;

    public CommonResult(Integer code, String message) {
        this(code, message, null);
    }

    public CommonResult(Integer code, String message, T o) {
        this.code = code;
        this.message = message;
        this.data = o;
    }

    public CommonResult(String message, T o) {
        this.message = message;
        this.data = o;
    }

    public static CommonResult success() {
        CommonResult result = new CommonResult();
        result.setMessage("操作成功");
        return result;
    }

    public static <T> CommonResult success(T o) {
        CommonResult<T> result = new CommonResult(200, "操作成功");
        result.setData(o);
        return result;
    }

    public static <T> CommonResult success(T o, String message) {
        CommonResult<T> result = new CommonResult(200, message);
        result.setData(o);
        return result;
    }

    public static CommonResult failure(String errorMsg) {
        CommonResult result = new CommonResult(BizCodeEnume.UNKNOW_EXCEPTION.getCode(), errorMsg, Boolean.FALSE);
        result.setSuccess(false);
        return result;
    }

    public static CommonResult failure(Integer code, String errorMsg) {
        CommonResult result = new CommonResult(code, errorMsg);
        result.setSuccess(false);
        result.setTraceId(MDC.get("traceId"));
        return result;
    }
    public static CommonResult failure(Integer code, String errorMsg,String cause) {
        CommonResult result = new CommonResult(code, errorMsg);
        result.setSuccess(false);
        result.setCause(cause);
        return result;
    }

    public static <T> CommonResult<T> failure(T o, String errorMsg) {
        CommonResult result = new CommonResult(BizCodeEnume.UNKNOW_EXCEPTION.getCode(), errorMsg, Boolean.FALSE);
        result.setData(o);
        result.setSuccess(false);
        return result;
    }

    public CommonResult() {
    }

    public Integer getCode() {
        return code;
    }

    public CommonResult setCode(Integer code) {
        this.code = code;
        return this;
    }

    public boolean isSuccess() {
        return success;
    }

    public CommonResult setSuccess(boolean success) {
        this.success = success;
        return this;
    }

    public String getMessage() {
        return message;
    }

    public CommonResult setMessage(String message) {
        this.message = message;
        return this;
    }

    public T getData() {
        return data;
    }

    public CommonResult setData(T data) {
        this.data = data;
        return this;
    }

    public String getCause() {
        return cause;
    }

    public void setCause(String cause) {
        this.cause = cause;
    }

    public String getTraceId() {
        return traceId;
    }

    public void setTraceId(String traceId) {
        this.traceId = traceId;
    }
}
